/**
 * 
 */
package cn.com.gzheroli.core.dbstyle;

import java.lang.annotation.Annotation;
import java.util.List;
import java.util.Map;

import org.beetl.sql.core.annotatoin.AssignID;
import org.beetl.sql.core.annotatoin.SeqID;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.OracleStyle;
import org.beetl.sql.core.kit.BeanKit;

/**  
* @类名: HeroOracleStyle  
* @描述: TODO(这里用一句话描述这个类的作用)  
* @作者: 李长荣 lichang_20011@163.com  
* @日期: 2017年9月11日  下午1:30:36
*@version V1.0  
*    
*/
public class HeroOracleStyle extends OracleStyle{
	 @Override
	    public String getPageSQL(String sql) {
	        //beetlT，beetl_rn 避免与sql重复
	        String pageSql = "SELECT * FROM "
	                + " ( "
	                + " SELECT beeltT.*, ROWNUM beetl_rn "
	                + " FROM ( \n" + sql + this.getOrderBy() + "\n )  beeltT "
	                + " WHERE ROWNUM <" + HOLDER_START + DBStyle.PAGE_END + HOLDER_END
	                + ") "
	                + "WHERE beetl_rn >= " + HOLDER_START + DBStyle.OFFSET + HOLDER_END;
	        return pageSql;
	    }


	    @Override
	    public void initPagePara(Map<String, Object> paras, long start, long size) {
	        long s = start + (this.offsetStartZero ? 1 : 0);
	        paras.put(DBStyle.OFFSET, s);
	        paras.put(DBStyle.PAGE_END, s + size);
	    }

	    @Override
	    public int getIdType(Class c,String idProperty) {
	    	 	List<Annotation> ans = BeanKit.getAllAnnoation(c, idProperty);
	        int idType = DBStyle.ID_ASSIGN; // 默认是自增长

	        for (Annotation an : ans) {
	            if (an instanceof SeqID) {
	                idType = DBStyle.ID_SEQ;
	                //seq 总是优先
	                break;
	            } else if (an instanceof AssignID) {
	                idType = DBStyle.ID_ASSIGN;
	            }
	        }

	        return idType;

	    }

	    @Override
	    public String getName() {
	        return "oracle";
	    }

	    @Override
	    public int getDBType() {
	        return DB_ORACLE;
	    }
}
